home *** CD-ROM | disk | FTP | other *** search
- -------------------------------------
- -- Example Program from the Manual --
- -------------------------------------
-
- sequence list, sorted_list
-
- function merge_sort(sequence x)
- -- put x into ascending order using a recursive merge_sort
- integer n, mid
- sequence newx, x1, x2
-
- n = length(x)
- if n = 0 or n = 1 then
- return x -- trivial case
- end if
- mid = floor(n/2)
- x1 = merge_sort(x[1..mid]) -- sort first half of x
- x2 = merge_sort(x[mid+1..n]) -- sort second half of x
-
- -- merge the two sorted halves into one
- newx = {}
- while length(x1) > 0 and length(x2) > 0 do
- if compare(x1[1], x2[1]) < 0 then
- newx = newx & x1[1]
- x1 = x1[2..length(x1)]
- else
- newx = newx & x2[1]
- x2 = x2[2..length(x2)]
- end if
- end while
- newx = newx & x1 & x2 -- get leftovers
- return newx
- end function
-
- procedure print_sorted_list()
- -- generate sorted_list from list
- list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2}
- sorted_list = merge_sort(list)
- ? sorted_list -- print the results
- end procedure
-
- print_sorted_list() -- this command starts the program running
-
-